package com.cskaoyan.javase.network._2tcp._v3;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 * 一个客户端多次向服务端发送消息,服务端接收并打印
 *
 * @since 15:16
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Client {
    public static void main(String[] args) throws IOException {
        // 1.创建客户端Socket对象,指定服务端的端口和IP地址
        Socket socket = new Socket("127.0.0.1", 8888);
        // 2.向服务端发送消息
        // 获取Socket对象中的输出流,向服务端发送数据
        OutputStream out = socket.getOutputStream();
        Scanner sc = new Scanner(System.in);
        // String msg = "";
        while (true) {
            String msg = sc.nextLine();
            out.write(msg.getBytes());
        }
        // 3.close关闭资源
        // socket.close();
    }
}
